//
//  SDHSportsInteDetailVC.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/7.
//

#import "SDHSportsInteDetailVC.h"
#import "SDHSportsWinRateView.h"
#import "SDHSportsInteDetailCell.h"
#import "SDHSportsNetRequest.h"
#import "SDHSportsRequest.h"
#import "SDHSportsInteDetailModel.h"
#import "MJExtension.h"
#import "SDHSportsDetailHeaderView.h"
#import "SDHSportsTestCell.h"
#import "SDHSportsTeamInfoVC.h"
#import "UIImageView+YQAdd.h"

const static CGFloat kTableHeaderHeight = 280;

@interface SDHSportsInteDetailVC () <UITableViewDelegate, UITableViewDataSource> {
    
}

@property (nonatomic, strong) UITableView *detailTable;
@property (nonatomic, strong) SDHSportsDetailHeaderView *headerView;
@property (nonatomic, strong) SDHSportsInteDetailDataModel *detailModel;

@end

@implementation SDHSportsInteDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Intelligence Details";
    
    [self initUI];
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)initUI {
    UITableView *table = [UITableView new];
    table.backgroundColor = [UIColor blackColor];
    table.delegate = self;
    table.dataSource = self;
    table.rowHeight = UITableViewAutomaticDimension;
    table.estimatedRowHeight = 100;
    [self.view addSubview:table];
    self.detailTable = table;
    
    _headerView = [SDHSportsDetailHeaderView new];
    [_headerView updateHeaderTeam:_model];
    _headerView.leftAvatarClick = ^{
        SDHSportsTeamInfoVC *vc = [[SDHSportsTeamInfoVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    };
    _headerView.rightAvatarClick = ^{
        SDHSportsTeamInfoVC *vc = [[SDHSportsTeamInfoVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    };
    self.detailTable.tableHeaderView = self.headerView;
    
    __weak typeof(self) weakSelf = self;
    [table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view).insets(UIEdgeInsetsZero);
    }];
}

- (void)endLoading {
    [MBProgressHUD hideHUD];
    [self.detailTable reloadData];
}

- (void)parseData:(NSDictionary *)obj {
    self.detailModel = [SDHSportsInteDetailDataModel mj_objectWithKeyValues:[obj objectForKey:@"data"]];
    [_headerView updateHeaderRatio:self.detailModel.data];
}

- (void)loadData {
    NSString *requestUrl = @"http://jsuywu8.com/guojitiyu/Tiyu1/getIntelligenceDetails";
    NSDictionary *dic = @{@"match_id":_model.match_id};
    SDHSportsRequest *request = [[SDHSportsRequest alloc] init];
    request.requestUrl = requestUrl;
    request.requestPara = dic;
    
    [MBProgressHUD showLoading];
    
    __weak typeof(self) weakSelf = self;
    [[SDHSportsNetRequest sharedRequest] sendRequest:request returnValueBlock:^(id  _Nonnull obj) {
        [weakSelf parseData:obj];
        
        [weakSelf endLoading];
    } errorBlock:^(id  _Nonnull obj) {
        [weakSelf endLoading];
    } failureBlock:^{
        [weakSelf endLoading];
    }];
    
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    self.detailTable.tableHeaderView.frame = CGRectMake(0, 0, kScreenWidth, kTableHeaderHeight);
}

#pragma mark - tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = 0;
    NSLog(@"self.detailModel: %@", self.detailModel);
    if (self.detailModel.news.guest_favorable_news.count > 0) {
        count = 2;
    }
    return count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 120;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SDHSportsInteDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SDHSportsInteDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    if (0 == indexPath.row) {
        cell.nameLb.text = _model.guest_en_name;
        [cell.avatarImg setImageWithURL:[NSURL URLWithString:_model.guest_logo] placeholderImage:[UIImage imageNamed:@"touxiang"]];
        [cell updateContent:self.detailModel.news.guest_favorable_news unfav:self.detailModel.news.guest_unfavorable_news];
    } else {
        cell.nameLb.text = _model.host_en_name;
        [cell.avatarImg setImageWithURL:[NSURL URLWithString:_model.host_logo] placeholderImage:[UIImage imageNamed:@"touxiang"]];
        [cell updateContent:self.detailModel.news.host_favorable_news unfav:self.detailModel.news.host_unfavorable_news];
    }
    return cell;
}

@end
